package com.hnichr.crm.biz.business.controller;

import cn.hutool.core.date.DateUtil;
import com.hnichr.crm.common.business.entity.CrmBusiness;
import com.hnichr.crm.common.business.service.ICrmBusinessService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 商机Controller
 *
 * @author Cheng
 * @date 2022-11-24
 */
@Slf4j
@Tag(name = "商机Controller", description = "商机")
@RestController()
@RequestMapping("/business")
public class CrmBusinessController {

    @Autowired
    private ICrmBusinessService crmBusinessService;

    /**
     * 查询商机列表
     */
    @GetMapping("/list")
    @Operation(summary = "商机", description = "分页查询", method = "GET")
    public Result<Page<CrmBusiness>> list(Criteria criteria,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        Page<CrmBusiness> page = crmBusinessService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 今日待跟进商机
     */
    @GetMapping("/listToday")
    @Operation(summary = "商机", description = "今日待跟进商机", method = "GET")
    public Result<Page<CrmBusiness>> listToday(Criteria criteria,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                          @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo) throws Exception {
        Pageable pageAble = PageRequest.of(pageNo - 1, pageSize);
        String loginUsername = SecurityUtils.getLoginUsername();
        criteria.and()
                .addCriterion("nextTime", Operator.LE, DateUtil.endOfDay(new Date()))
                .addCriterion("ownerUserId", Operator.EQ, loginUsername)
                .end();
        Page<CrmBusiness> page = crmBusinessService.page(criteria, pageAble);
        return Result.OK(page);
    }

    /**
     * 添加
     */
    @PostMapping(value = "/add")
    @Operation(summary = "商机-添加", description = "商机-添加", method = "POST")
    public Result<?> add(@RequestBody CrmBusiness crmBusiness) {
        crmBusiness.setBatchId(UUID.randomUUID().toString());
        if (StringUtils.isEmpty(crmBusiness.getOwnerUserId())) {
            crmBusiness.setOwnerUserId(SecurityUtils.getLoginUsername());
        }
        if (Objects.isNull(crmBusiness.getDeptId())) {
            Set<String> deptSet = SecurityUtils.getDeptCode();
            if(!Objects.isNull(deptSet)){
                List<String> deptlist = new ArrayList<String>(deptSet);
                if(deptlist!=null && deptlist.size()>0){
                    crmBusiness.setDeptId(deptlist.get(0));
                    crmBusiness.setCreateDept(deptlist.get(0));
                }
            }
        }
        crmBusinessService.save(crmBusiness);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param crmBusiness
     * @return
     */
    @Operation(summary = "商机-编辑", description = "商机-编辑", method = "PUT")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CrmBusiness crmBusiness) {
        crmBusinessService.update(crmBusiness);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    @Operation(summary = "商机-通过id删除", description = "商机-通过id删除")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        crmBusinessService.delete(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping(value = "/deleteBatch")
    @Operation(summary = "商机-批量删除", description = "商机-批量删除")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/queryById")
    @Operation(summary = "商机-通过id查询", description = "商机-通过id查询")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        CrmBusiness crmBusiness = crmBusinessService.getById(id);
        if (crmBusiness == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(crmBusiness);
    }


}
